<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body{
            font:12px/1.5 arial;
            text-align:center;
        }
        .f-text{
            width:50px;
            border:1px solid #ccc;
            background:#f0f0f0;
            font-family:inherit;
            padding:3px;
            margin-right:10px;
        }
        </style>
</head>
<body>
    <input type="text" class="f-text" /><input type="button" value="是否为两位数" />
</body>
</html>
<script>
    var aInput = document.getElementsByTagName("input");
    var aSpan = document.getElementsByTagName("span")[0];
    var i = 0;
    
    aInput[0].onkeyup = function ()
    {
        this.value = this.value.replace(/[^\d]/,"")
    }
    
    aInput[1].onclick = function ()
    {
        /*test() 方法用于检测一个字符串是否匹配某个模式*/
        (aInput[0].value == "") ?
        alert("请输入数字！") :
        alert(/^\d{2}$/.test(parseInt(aInput[0].value)) ? "√ 是两位数" : "这是" + aInput[0].value.length + "位数");
    }
</script>